using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace SunshineFarmWord
{
    public class PlayerStatesIcon : MonoBehaviour
    {
        [Header("图标根节点")]
        [field: SerializeField, Tooltip("图标根节点")]
        private GameObject iconNode;
        [Header("图标列表")]
        [field: SerializeField, Tooltip("图标列表")]
        private List<Sprite> spriteRendererList;
        [field: SerializeField]
        private Dictionary<PlayerStateReusableData.Collider2DTagEnum, Sprite> spriteRendererDict = new Dictionary<PlayerStateReusableData.Collider2DTagEnum, Sprite>();
        [Header("图标")]
        [field: SerializeField]
        private SpriteRenderer icon;
        [field: SerializeField, Header("玩家对象")]
        private Player player;

        private PlayerStateReusableData _playerStateReusableData;
        private PlayerSO _playerSOData;
        void Start()
        {
            icon.sprite = null;
            _playerStateReusableData = player.GetPlayerReusableData();
            _playerSOData = player.GetPlayerData();

            foreach (PlayerSO.PlayerStateUISpriteData item in _playerSOData.playerStateUISpriteData)
            {
                spriteRendererDict.Add(item.tagEnum, item.sprite);
            }

        }

        void Update()
        {
            //
            UpdatePlayerStateIcon();
        }

        #region Main function 
        private void SetActiveToNode(bool active)
        {
            if (iconNode.activeSelf == active) return;
            iconNode.SetActive(active);
            if (!active) icon.sprite = null;
        }

        //更新图标
        private void UpdatePlayerStateIcon()
        {
            foreach (PlayerStateReusableData.Collider2DTagEnum item in spriteRendererDict.Keys)
            {
                if (_playerStateReusableData.IsInState(item))
                {
                    SetActiveToNode(true);
                    icon.sprite = spriteRendererDict[item];
                    return;
                }
            }

            SetActiveToNode(false);
        }
        #endregion
    }
}
